package com.why.service.impl;

import com.why.dao.ProductImgDao;
import com.why.dao.ProductSkuDao;
import com.why.entity.ProductImgEntity;
import com.why.entity.ProductSkuEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.why.common.utils.PageUtils;
import com.why.common.utils.Query;

import com.why.dao.ProductDao;
import com.why.entity.ProductEntity;
import com.why.service.ProductService;


@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductDao, ProductEntity> implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Autowired
    private ProductSkuDao productSkuDao;

    @Autowired
    private ProductImgDao  productImgDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductEntity> page = this.page(
                new Query<ProductEntity>().getPage(params),
                new QueryWrapper<ProductEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ProductEntity> queryRecommendsProduct(Integer length) {
        return productDao.selectRecommends(length);
    }

    @Override
    public Map queryProductDetail(Long productId) {

        QueryWrapper<ProductEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("product_status",1);
        wrapper.eq("product_id",productId);

        ProductEntity product = this.productDao.selectOne(wrapper);

        if(product!=null){
            //查询图片
            QueryWrapper<ProductImgEntity> imgWrapper = new QueryWrapper<>();
            imgWrapper.eq("item_id",productId);
            List<ProductImgEntity> imgEntityList = productImgDao.selectList(imgWrapper);

            //查询sku
            QueryWrapper<ProductSkuEntity> skuWrapper = new QueryWrapper<>();
            List<ProductSkuEntity> skuEntityList = productSkuDao.selectList(skuWrapper);
            //封装结果集
            HashMap data = new HashMap(4);

            data.put("product",product);
            data.put("imgs",imgEntityList);
            data.put("productSkus",skuEntityList);
            return  data;
        }

        return null;
    }

}